package cn.net.xyan.yypt.dto

import cn.net.xyan.blossom.kotlin.StatusAndMessageError
import cn.net.xyan.yypt.dto.protal.*

fun <B:LKAlipayRspBody> LKAlipayRsp<B>.readFromLinkingCloud(response:LinkingCloudResponse<*>){
    this.body?.responseResult?.isSuccess = if (response.code == InvokeSuccess) 1 else response.code?.toInt()
    this.body?.responseResult?.message = response.msg
}

fun  LinkingCloudResponse<*>.toRsp(): LKAlipayRsp<LKAlipayRspBody> {


    val bizContent = this.bizContent
    val body:LKAlipayRspBody = when(bizContent){
        is QRCodeRsp -> {
            val b = QrcodeTradePayResultBody()
            b.qrcodeTradePayResult.outTradeNo = bizContent?.outTradeNo
            b.qrcodeTradePayResult.twoDimensionCode = bizContent?.qrCode
            b
        }
        is RefundOrderRsp -> {
            val b = BarcodeTradeCancelResultBody()
            b.barcodeTradeCancelResult.outTradeNo = bizContent?.outTradeNo
            b.barcodeTradeCancelResult.tradeNo = bizContent?.tradeNo
            b
        }
        is QueryOrderRsp -> {
            val b = TradeStatusModelBody()
            b.tradeStatusModel.tradeStatus = bizContent?.tradeStatus
            b
        }
        is BarCodeRsp -> {
            val b = BarcodeTradePayResultBody()
            b.barcodeTradePayResult.outTradeNo = bizContent?.outTradeNo
            b.barcodeTradePayResult.tradeNo = bizContent?.tradeNo
            b.barcodeTradePayResult.amount = bizContent?.totalAmount.toString()
            b
        }
        else ->{
            throw StatusAndMessageError("不支持的bizContent 类型:${bizContent?.javaClass?.name}")
        }
    }




    val rsp =  LKAlipayRsp.makeRsp(body)

    rsp.readFromLinkingCloud(this)

    return rsp
}


